package com.cdplife.ci.modular.system.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.cdplife.ci.core.common.annotion.BussinessLog;
import com.cdplife.ci.core.common.annotion.FormToken;
import com.cdplife.ci.core.common.annotion.Permission;
import com.cdplife.ci.core.common.constant.dictmap.TenantDict;
import com.cdplife.ci.core.common.exception.BizExceptionEnum;
import com.cdplife.ci.core.log.LogObjectHolder;
import com.cdplife.ci.core.shiro.ShiroKit;

import org.springframework.web.bind.annotation.RequestParam;

import com.cdplife.ci.modular.system.model.Insurance;
import com.cdplife.ci.modular.system.model.Tenant;
import com.cdplife.ci.modular.system.service.ITenantService;
import com.cdplife.ci.modular.system.warpper.TenantWarpper;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-11-07 14:27:44
 */
@Controller
@RequestMapping("/tenant")
public class TenantController extends BaseController {

    private String PREFIX = "/system/tenant/";

    @Autowired
    private ITenantService tenantService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tenant.html";
    }

    /**
     * 跳转到添加
     */
    @FormToken(save=true)
    @RequestMapping("/tenant_add")
    public String tenantAdd() {
        return PREFIX + "tenant_add.html";
    }

    /**
     * 跳转到修改
     */
    @FormToken(save=true)
    @RequestMapping("/tenant_update/{tenantId}")
    public String tenantUpdate(@PathVariable Integer tenantId, Model model) {
        Tenant tenant = tenantService.selectById(tenantId);
        model.addAttribute("item",tenant);
        LogObjectHolder.me().set(tenant);
        return PREFIX + "tenant_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	List<Map<String, Object>> result = tenantService.selectTenantList(condition);
        return new TenantWarpper(result).wrap();
    }

    /**
     * 新增
     */
    @FormToken(remove=true)
    @Permission
    @BussinessLog(value = "添加客户", key = "tenantName,tenantCode", dict = TenantDict.class)
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Tenant tenant) {
    	 
    	// 判断客户编号是否重复
        Tenant theTenant = tenantService.getByTenantCode(tenant.getTenantCode());
        if (theTenant != null) {
            throw new ServiceException(BizExceptionEnum.TENANT_ALREADY_REG);
        }
        
    	//新增获取当前操作者id
        tenant.setCreatedBy(String.valueOf(ShiroKit.getUser().getId()));
    	
        tenantService.insert(tenant);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @Permission
    @BussinessLog(value = "删除客户", key = "tenantName,tenantCode", dict = TenantDict.class)
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tenantId) {
    	//删除逻辑
    	//to do
        tenantService.deleteById(tenantId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @FormToken(remove=true)
    @Permission
    @BussinessLog(value = "编辑客户", key = "tenantName,tenantCode", dict = TenantDict.class)
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Tenant tenant) {
    	if (ToolUtil.isEmpty(tenant) || tenant.getId() == null) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
    	
    	// 判断客户编码是否重复
    	Tenant theTenant = tenantService.getByTenantCode(tenant.getTenantCode());
        if (theTenant != null && !theTenant.getId().equals(tenant.getId())) {
        	throw new ServiceException(BizExceptionEnum.TENANT_ALREADY_REG);
        }
        //更新获取当前操作者id作为更新者
        tenant.setLastModifiedBy(String.valueOf(ShiroKit.getUser().getId()));
    	
        tenantService.updateById(tenant);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{tenantId}")
    @ResponseBody
    public Object detail(@PathVariable("tenantId") Integer tenantId) {
        return tenantService.selectById(tenantId);
    }
}
